package com.cf.controller;

import com.cf.entity.Item;
import com.cf.entity.Result;
import com.cf.service.FocusServeice;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class FocusController {
    @Autowired
    private FocusServeice focusServeice;

    //查询总关注条数
    @RequestMapping("/selectCountByitemid")
    public Object selectCountByitemid(long iid){
        int i = focusServeice.selectCountByitemid(iid);
        return new Result(200,"查询成功",i);
    }

    //添加关注
    @RequestMapping("/insertFocus")
    public Object insertFocus(long userid, long iid){
        int i = focusServeice.insertFocus(userid, iid);
        if(i!=0){
            return new Result(200,"添加成功",null);
        }else {
            return new Result(400,"添加失败",null);
        }
    }

    //取消关注
    @RequestMapping("/deleteFocus")
    public Object deleteFocus(long userid, long iid){
        int i = focusServeice.deleteFocus(userid, iid);
        if(i!=0){
            return new Result(200,"删除成功",null);
        }else {
            return new Result(400,"删除失败",null);
        }
    }


    @RequestMapping("/selectItemByUserIds")
    public Object selectItemByUserIds(@RequestParam(defaultValue = "1") int pagenum, int pagesize, long userid){

        PageInfo<Item> itemPageInfo = focusServeice.selectItemByUserIds(pagenum, pagesize, userid);
        if (itemPageInfo.getList().size()!=0){
            return new Result(200,"查询成功",itemPageInfo);
        }else {
            return new Result(400,"查询失败",null);
        }
    }

    @RequestMapping("/selectItemByIdAndwu")
    public Object selectItemByIdAndwu(long userid){
        List<Item> items = focusServeice.selectItemByIdAndwu(userid);
        List list = new ArrayList();
        for (int i = 0; i < items.size(); i++) {
            Map map = new HashMap<>();
            map.put("value",items.get(i).getGzcount());
            map.put("name",items.get(i).getIname());
            list.add(map);
        }
        if (items.size()!=0){
            return new Result(200,"查询成功",list);
        }else {
            return new Result(400,"查询失败",null);
        }
    }
    @RequestMapping("/updategz")
    public Object updategz(long userid,long iid){
        int updategz = focusServeice.updategz(userid, iid);
        System.out.println(updategz);
        return new Result(200,"成功",updategz);

    }
    @RequestMapping("/selectByuidAndiid")
    public Object selectByuidAndiid(long userid,long iid){
        int num = focusServeice.selectByuidAndiid(userid, iid);
        if (num!=0){
            return new Result(200,"查询成功",num);
        }else {
            return new Result(400,"查询失败",null);
        }

    }
}
